// Dynamic memory management

#include <cstdlib>
#include <iostream>
using namespace std;


int main( void ) {
	int* pm = ( int* )malloc( 4 );
	cout << "*pm = " << *pm << endl;
	free( pm );
	pm = NULL;

	// c++ have a new operator which is called new and delete
	int* pi = new int( 100 );	// It can initialize the pi
	cout << "*pi = " << *pi << endl;
	delete pi;
	pi = NULL;

	// The operteror can initialize an array
	int* parr = new int[4]{ 10, 20, 30, 40 };
	for( int i=0; i<4; i++) {
		printf("parr[%d] = %d\n", i, *(parr+i));
	}
	// You should delete the memory by the array method
	// The new operator will generate a memory another to keep how much elem the array content
	delete[] parr;
	parr = NULL;

	int(* ps)[4] = new int[3][4];
	delete[] ps;
	ps = NULL;

	try {
		new int[0xfffffffffffff];
	}
	catch(...) {
	
	}

	return 0;
}

